<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dynamiclist {
/*-----------------------------------------------------------------------------------------------
get_dynamic_classfromcourse_list_js
Sets up arrays and handles dynamic list generation of class lists for a given course
-----------------------------------------------------------------------------------------------*/
    function get_dynamic_classfromcourse_list_js($fid, $frm, $tid, $inline_script=TRUE) {
        $CI = &get_instance();
        
        $fillfunc=$frm."fillClasses";
        $actfunc=$frm."actionSelectCO";
$start = '<SCRIPT LANGUAGE="JavaScript">';
$end = '</script>';
if(!$inline_script){
    $start = '';
    $end = '';
}
        // First place the javascript
        $js = <<<EOF
        $start
        function $actfunc(fid,pid,cid) {   
            $fillfunc(); 
        }

        function fillArr(link, name){
            this.link = link;
            this.name = name;
            return this;
        }                
        function newOption(value, text) {
            var Opt        = new Option();
            Opt.value    = value;
            Opt.text    = text;
            return Opt;
        }

        function $fillfunc(){
            var form = document.forms.$frm;
            var course = form.cid.options[form.cid.selectedIndex].value;

            if (course != ""){ 
                var courseArr = new Array();
                courseArr = eval('arr$frm'+course);
                form.clid.disabled = false;
                form.clid.options.length = 0;
                form.clid.options[0] = newOption("", "< choose one >" );
                for (i=0; i<courseArr.length; i++){
                    form.clid.options[i+1] = newOption(courseArr[i].link, courseArr[i].name);
                }
            } else { 
                form.clid.options.length = 0;
                form.clid.options[0] = newOption("", "< choose two >");
                form.clid.disabled = true;
            }
        }
EOF;

        // Now add class lists for this course
        //$qry = "select name, product_id from product where type='CO' and admin_id=".$fid;
        $CI->db->select(PRODUCT_NAME.' AS name, '.PRODUCT_PRODUCT_ID.' AS product_id');
        $CI->db->where(PRODUCT_TYPE, 'CO');
        $CI->db->where(PRODUCT_FRANCHISE_ID, $fid);
        $res = $CI->db->get(PRODUCT)->result_array();
        foreach($res as $row){
            // Generate a JS array name per course
            $arrname = "arr".$frm.$row['product_id'];
            $js.="var ".$arrname." = new Array();\n";
    
            // Populate the array with the locations for that company
            $qry2 ='select l.name as loc_name, ci.class_id, ci.start_time, ci.day
                    from course_instance ci, location l 
                    where ci.admin_id = l.admin_id 
                    and ci.location_id = l.location_id
                    and ci.product_id = '.$row['product_id'].'
                    and ci.term_id = '.$tid.'
                    and ci.admin_id = '.$fid;

            if ($frm=="frmMakeup") {
                $CI->load->module_model('blocks.utils_model');
                $value = $CI->utils_model->get_makeup($fid);
            } else {
                $value="No";
            }
            if($value == "No"){
            $qry2 .= ' AND ci.status<>\'red\'';
            }
            $res2 = $CI->db->query($qry2)->result_array();

            $arr=0;
            foreach($res2 as $row2){
                $list_item = $row2['loc_name']." : ".substr($row2['day'],0,3)." ".date("h:i",strtotime($row2['start_time']));
                $js.=$arrname."[".$arr."] = {link:\"".$row2['class_id']."\", name: \"".$list_item."\"};\n";
                $arr++;
            }
        }
        $js.="\n$end\n";
;
        return $js;
    } // end function
        
}
?>
